/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package us.levk.tools.collections;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;


/**
 * @author levk
 *
 */
public class NonDestructiveSet<E> extends AbstractSet<E> {

  private final Set<E> wrapped;

  public NonDestructiveSet (Set<E> wrapped) { this.wrapped = wrapped; }

  /* (non-Javadoc)
   * @see java.util.AbstractCollection#iterator()
   */
  @Override public Iterator<E> iterator () {
    return new Iterator<E> () {
      private final Iterator<E> that = wrapped.iterator ();

      @Override public boolean hasNext () { return that.hasNext (); }

      @Override public E next () { return that.next (); }

      @Override public void remove () { throw new UnsupportedOperationException (); }
    };
  }

  /* (non-Javadoc)
   * @see java.util.AbstractCollection#size()
   */
  @Override public int size () { return wrapped.size (); }

  /* (non-Javadoc)
   * @see java.util.AbstractCollection#add(java.lang.Object)
   */
  @Override public boolean add (E e) {
    if (wrapped.contains (e)) throw new IllegalArgumentException (e + " is lready present in this set");
    else return wrapped.add (e);
  }

}
